<cfif Not IsDefined("session.arrTri")>
	<cfoutput>
		Experiment Error!! -- arrTri
	</cfoutput>
	<cfabort>
</cfif>

<cfif Not IsDefined("session.count")>
	<cfoutput>
		Experiment Error!! -- count
	</cfoutput>
	<cfabort>
</cfif>

<cfif Not IsDefined("id")>
	<cfoutput>
		Experiment Error!! -- id
	</cfoutput>
	<cfabort>
</cfif>

<cfquery name = "trialinfo" datasource="exp">
	SELECT s.*, t.*, stimType.*
	FROM stimuli s, trial t, stimulitype stimType
	WHERE t.stimid = s.id
	AND s.stimulitypeid = stimType.id
	AND t.id = <cfqueryparam cfsqltype="cf_sql_varchar" value="#session.arrTri[session.count]#">
</cfquery>

<cfif IsDefined("form.subAns")>
	<!--- checks to see if the participants answer is of the correct type --->
	<cfset name_input = "hid_pans_" & id>
	<cfif IsDefined("form." & name_input)>
		<cfset temp = form['#name_input#']>
		<cfif IsDefined("form." & temp)>
			<cfset pans = form['#temp#']>
			<cfif trialinfo.isnumeric>
				<cfif !IsNumeric(#pans#)>
					<cfset session.message = "Answer must be a number and must contain no letters or spaces!">
					<cflocation url="/ecada/exppageV2.cfm?id=#id#">
					<cfabort>
				</cfif>
			</cfif>
		<cfelse>
			<cfoutput>
				Unknown Error! - form.##temp## not defined ---- temp = #temp# 
			</cfoutput>
			<cfabort>
		</cfif>
	<cfelse>
		<cfoutput>
			Unknown Error! -- form.##name_input## not defined ---- #name_input#
		</cfoutput> 
		<cfabort>
	</cfif>

	<!--- Take a second time stamp --->
	<cfquery datasource="exp">	
		INSERT INTO givesanswer( participantid, trialid, answer, hour, minute, second,  hour2, minute2, second2 )
		VALUES
			(   
				<cfqueryparam cfsqltype="cf_sql_integer" value="#session.pid#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#trialinfo.id#">,
				<cfqueryparam cfsqltype="cf_sql_varchar" value="#pans#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#hour(session.TimeStamp)#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#minute(session.TimeStamp)#">,
				<cfqueryparam cfsqltype="cf_sql_integer" value="#second(session.TimeStamp)#">,
				#hour(Now())#,
				#minute(Now())#,
				#second(Now())#
			)
	</cfquery>

    <cfset session.count += 1>	
	<cfif session.count eq ArrayLen(session.arrTri) + 1>
		<!--- is there an ending questionnaire? --->
		<cfquery name="questend" datasource="exp">
			SELECT id
			FROM questionnaire
			WHERE expid = #session.expid#
			AND isend = 1
		</cfquery>
		<cfif questend.recordcount gt 0>
			<!---goto end questionnaire--->
			<cflocation url="/ecada/quest.cfm?Qid=#questend.id#">  
		<cfelse>
			<!--- redirects to last page --->
			<cflocation url="/ecada/thanks.cfm?id=#id#">
		</cfif>
	<cfelse>
		<cflocation url="/ecada/exppageV2.cfm?id=#id#">
	</cfif>

<cfelse>
	<!---Take a timestamp of the when the question was started--->
	<cfset session.TimeStamp = Now()>
	
	<cfinclude template= "/ecada/pheader.cfm">

	<cfquery datasource="exp" name="file">
		SELECT *
		FROM filestore
		WHERE id = #trialinfo.fileid#
	</cfquery>

	<!--- THIS IS WHAT THE PAGE WILL OUTPUT --->
	<!---Simply add a condition for new stimuli types in the database--->
	<cfinclude template= "#trialinfo.typeinclude#">	
	
	--->
	<cfoutput> 
		<div>
			<script type="text/javascript">
				$(document).ready(function(){
					$('##Qbox').corner();
					$('##Abox').corner();
				});
			</script>
			<div id="Qbox" class="ui-widget-content" style="width: 100%; margin-left: auto; margin-right: auto;">
				<table>
					<tr>
						<td style="border-right: 2px solid ##A6C9E2;">
							<p style="margin: 0; margin-right: 10px; margin-left: 10px;">
								Question
							</p>
						</td>
						<td>
							<p style="margin: 0; margin-left: 10px;">
								#trialinfo.question#
							</p>
						</td>
					</tr>
				</table>      
			</div>
			
			<form style="margin: 0;" method = "post" id="form1" action = "/ecada/exppageV2.cfm?id=#id#">   
				<div id="Abox" class="ui-widget-content" style="margin-left: auto; margin-right: auto; width: 100%;">
					<table>
						<tr>
							<td style="border-right: 2px solid ##A6C9E2;">
								<p style="margin: 0; margin-right: 7px; margin-left: 10px;">
									Answer &nbsp;&nbsp;
								</p>
							</td>
							<td>
								<cfset chars = "0123456789abcdefghiklmnopqrstuvwxyz">
								<cfset strLength = 6>
								<cfset randout = ArrayNew(1)>

								<cfloop from="1" to="#strLength#" index="i">
									<cfset rnum = ceiling(rand() * len(chars))>
									<cfif rnum EQ 0 ><cfset rnum = 1></cfif>
									<cfset ArrayAppend(randout, mid(chars, rnum, 1))>
								</cfloop>
								
								<cfset randout = ArrayToList(randout, "")>
							
								<input type="hidden" name="hid_pans_#id#" id="hid_pans_#id#" value="a_#randout#">
								
								<div style="margin: 0; margin-left: 10px;">
									<!---Simply add a condition for new answer types--->
									<cfif trialinfo.answerbox>
										<cfinclude template= "/ecada/Participation/answerbox.cfm">
									<cfelseif trialinfo.answerradio>
										<cfinclude template= "/ecada/Participation/radio.cfm">
									<cfelseif trialinfo.answerlikert>
										<cfinclude template= "/ecada/Participation/likert.cfm">
									</cfif>
								</div>
							</td>
						</tr>
					</table>     
				</div>
				<div style="width: 100%">
					<center>
						<button class="ui-state-default ui-corner-all" type="submit" name="subAns" id="subAns" style="margin: 20px; width: 150px; height: 50px;" >
							<strong>Continue</strong>
						</button>
					</center>
				</div>
			</form>
		</div>
		<div id="dialog-confirm" style="display: none;" title="Audio Question?">
			<p>
				<span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>
				This following question is an audio based experiment, before continuing can you please make sure that your headphone are plugged in or that your speakers are on and finally make sure your volume is up; then press continue.
			</p>
		</div>
		</body>
		</html>
    </cfoutput>
</cfif>   
<!--- END OF PAGE OUTPUT --->